package draftMate;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import draftMate.player.Hitter;
import draftMate.player.Pitcher;


public class DraftMate {
	final static int NUMBATS = 325; // subject to change
	final static int NUMARMS = 225;
	
	private static Hitter hitterPool[];
	private static Pitcher pitcherPool[];
	private Team teams[];
	
	public static void importPitchers(String filename) {
		String name;
		int wins;
		int ER;
		int walks;
		int hits;
		int ip;
		int k;
		int saves;
		int i = 0;
		File f = null;
		BufferedReader br = null;
		String line = null;
		StringTokenizer st = null;
		hitterPool = new Hitter[NUMBATS];
		pitcherPool = new Pitcher[NUMARMS];
		
		try {
			f = new File(filename);
			br = new BufferedReader(new FileReader(f));
			br.readLine(); // gets rid of first line
			
			while((line = br.readLine()) != null ) {
				st = new StringTokenizer(line, ",\"");
				name = st.nextToken().toLowerCase();
				wins = Integer.parseInt(st.nextToken());
				
				for (int j = 0; j < 4; j++) {
					st.nextToken(); // skips L, ERA, GS, G
				}
				
				saves = Integer.parseInt(st.nextToken());
				ip = Integer.parseInt(st.nextToken());
				hits = Integer.parseInt(st.nextToken());
				ER = Integer.parseInt(st.nextToken());
				
				st.nextToken(); // skip HR
				
				k = Integer.parseInt(st.nextToken());
				walks = Integer.parseInt(st.nextToken());
				
				Pitcher toAdd = new Pitcher(name, wins, ER, walks, hits, ip, k, saves);
				pitcherPool[i++] = toAdd;
			}
			
			Arrays.sort(pitcherPool);	
		} catch (IOException e) {
			System.err.println("An error occured while importing pitchers from " +
								filename);
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				System.err.println("Problem closing file input stream.");
			}
		}
	}
	
	public static void importHitters(String filename) {
		String name;
		int ab;
		int hits;
		int hr;
		int runs;
		int rbi;
		int steals;
		
		int i = 0;
		File f = null;
		BufferedReader br = null;
		String line = null;
		StringTokenizer st = null;
		
		try {
			f = new File(filename);
			br = new BufferedReader(new FileReader(f));
			br.readLine(); // gets rid of first line
			
			while((line = br.readLine()) != null ) {
				st = new StringTokenizer(line, ",\"");
				name = st.nextToken().toLowerCase();
				
				st.nextToken(); // skips games
				
				ab = Integer.parseInt(st.nextToken());
				hits = Integer.parseInt(st.nextToken());
				
				for (int j = 0; j < 2; j++) {
					st.nextToken(); // skips 2B, 3B
				}
				
				hr = Integer.parseInt(st.nextToken());
				runs = Integer.parseInt(st.nextToken());
				rbi = Integer.parseInt(st.nextToken());
				
				for (int j = 0; j < 3; j++) {
					st.nextToken(); // skip walks, Ks, HBP
				}
				
				steals = Integer.parseInt(st.nextToken());
				
				Hitter toAdd = new Hitter(name, ab, hits, hr, runs, rbi, steals);
				hitterPool[i++] = toAdd;
			}
			
			Arrays.sort(hitterPool);
		} catch (IOException e) {
			System.err.println("An error occured while importing pitchers from" +
								filename);
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				System.err.println("An error occured while closing the file stream");
			}
		}
	}
	
	
	public void addTeam(String teamName) {
		Team toAdd = new Team(teamName);
	}
	
	public static String getProjections(Team team) {
		return ("Wins\tKs\tERA\tWHIP\tSaves\tRuns\tRBIs\tHRs\tSteals\tAvg\n" +
				team.getpWins() + "\t" + team.getpK() + "\t" + team.getpERA() +
				"\t" + team.getpWHIP() + "\t" + team.getpSaves() + "\t" +
				team.getpRuns() + "\t" + team.getpRBI() + "\t" + team.getpHR() +
				"\t" + team.getpSteals() + "\t" + team.getpAB());
	}
	
	public static void main(String args[]) {
		JMenuBar menuBar;
		JMenu menu;
		JMenuItem openPitchers, openHitters, openDraft, quit, save;
		
		menuBar = new JMenuBar();
		menu = new JMenu("File");
		menuBar.add(menu);
		openPitchers = new JMenuItem("Load pitcher projections");
		openHitters = new JMenuItem("Load hitter projections");
		openDraft = new JMenuItem("Open existing draft");
		save = new JMenuItem("Save");
		quit = new JMenuItem("Quit");
		menu.add(openPitchers);
		menu.add(openHitters);
		
		JFrame frame = new JFrame("Draft Mate nigga");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new Button("add"), BorderLayout.CENTER);
		frame.getContentPane().add(new List(20), BorderLayout.PAGE_END);
		frame.pack();
		frame.setVisible(true);
		
		importPitchers("pitchersLeaderBoard.csv");
		importHitters("hittersLeaderBoard.csv");
		
		for (int i = 0; i < pitcherPool.length; i++) {
			System.out.println(pitcherPool[i].toString());
		}
		
		System.out.println("\n\n\n");
		
		for (int i = 0; i < hitterPool.length; i++) {
			System.out.println(hitterPool[i].toString());
		}

	}
}
